home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
toolfix.arc
/
SORT1.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1985-08-07
|
4KB
|
130 lines
{$C-}
program SortAFile;
{
TURBO DATABASE TOOLBOX DEMONSTRATION PROGRAM:
Demonstrates how to sort a file of records.
Modified: 08/07/85
This program takes the CUSTOMER.DTA file, sorts the the records by
the Number field, and displays the sorted records on the screen.
}
type
NameString = string[25];
CustRec = record
Number: integer;
Name: NameString;
Addr: string[20];
City: string[12];
State: string[3];
Zip: string[5];
end;
CustFileType = file of CustRec;
var
CustFile : CustFileType;
Customer : CustRec;
Results : integer;
{$I SORT.BOX }
procedure OpenFile(var f : CustFileType; Name : NameString);
{ Display welcome screen, open data file }
begin
ClrScr;
Writeln('TURBO-SORT DEMONSTRATION PROGRAM');
Writeln;
Writeln('Opening data file');
Assign(f, Name);
{$I-}
Reset(f);
{$I+}
if IOresult <> 0 then
begin
Writeln(^G, ' -- Cannot find ', Name);
Halt; { abort program }
end;
end; (* OpenFile *)
procedure Inp;
{ This procedure is forward declared in SORT.BOX. It sends a stream
of records to the sort routine.
}
var
rec : integer; { # of records read from data file }
begin
rec := 0;
Writeln;
Writeln('Input routine -- sending ', FileSize(CustFile),
' records to sort:');
repeat
rec := rec + 1;
Write(#13, rec:6);
Read(CustFile,Customer);
SortRelease(Customer);
until EOF(CustFIle);
Writeln;
Writeln;
Writeln('Done with input -- sorting ',
FileSize(CustFile), ' records . . .');
end; { Inp }
function Less;
{ This boolean function is forward declared in SORT.BOX and has
two parameters, X and Y. Because this function is called so
often, the number of statements in this function should be
kept to a minimum.
}
var
FirstCust: CustRec absolute X;
SecondCust: CustRec absolute Y;
begin
Less := FirstCust.Number < SecondCust.Number; { define sort order }
end; { Less }
procedure OutP;
{ This procedure is forward declared in SORT.BOX. It
retrieves the sorted objects one-by-one.
}
var
i : integer;
begin
repeat
if KeyPressed then Halt; { Key touched? Stop program }
SortReturn(Customer);
with Customer do
begin
Write(Number, ' ', Name,' ');
for i := Length(Name) to 25 do Write(' ');
Write(Addr);
for i := Length(Addr) to 20 do Write(' ');
Write(City);
for i := Length(City) to 12 do Write(' ');
Writeln(State,' ', Zip);
end; { with }
until SortEOS;
end; (* OutP *)
procedure DisplayResults(results : integer);
begin
Writeln;
Writeln;
case Results of { display sort results }
0 : Writeln('Done with sort and display.');
3 : Writeln('Error: not enough memory to sort');
8 : Writeln('Error: illegal item length.');
9 : Writeln('Error: can only sort ', MaxInt, ' records.');
10 : Writeln('Error: disk full or disk write error.');
11 : Writeln('Error: disk error during read.');
12 : Writeln('Error: directory full or invalid path name');
end; (* case *)
end; (* DisplayResults *)
begin { program body }
OpenFile(CustFile, 'CUSTOMER.DTA'); { open data file to sort }
Results := TurboSort(SizeOf(CustRec)); { sort the file of records }
DisplayResults(Results); { display sort results }
end.